/******************************************************************************
 * @File name   :      UniqueTokenTag.java
 *
 * @Author      :      Wu Jianxi
 *
 * @Date        :      2011-9-29
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-9-29 ä¸‹å�ˆ01:34:41        Wu Jianxi    1.0            Initial Version
 * 2011-10-14 ä¸‹å�ˆ3:51:02        Wu Jianxi    1.1            modify unique token
 *****************************************************************************/
package com.cap.bts.framework.auth.tags;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.cap.bts.commonfunc.cache.constants.CachePrefix;
import com.cap.bts.framework.cache.util.CacheUtil;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.util.CookieUtils;

/**
 *
 */
public class UniqueTokenTag extends BodyTagSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2011-9-29
     * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
     **/
    @Override
    public int doEndTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

        String userCode = CookieUtils.getCookie(request, null, "user");

        //if user code is not empty
        if (null != userCode && !"".equals(userCode.trim())) {
            String sessionToken = CachePrefix.SESSION_TOKEN + CachePrefix.DIVIDE + userCode;
            String tokenStr = UUID.randomUUID().toString();
            CacheUtil.putCache(sessionToken, tokenStr);
            
            //å¦‚æžœæ”¾å…¥ç¼“å­˜å¤±è´¥ï¼Œåˆ™ä¸�è¿›è¡Œéš�è—�çš„tokenè®¾ç½®ã€‚
            if(!CacheUtil.containsCache(sessionToken)){
                return EVAL_PAGE;
            }
            
            try {
                pageContext.getOut().write(buildTokenElement(tokenStr));
            } catch (IOException e) {
                if(CacheUtil.containsCache(sessionToken)){
                    CacheUtil.removeCache(sessionToken);
                }
                throw new JspTagException("write unique token element error.");
            }
        }
        return EVAL_PAGE;
    }

    public String buildTokenElement(String tokenStr) {
        StringBuilder sb = new StringBuilder();
        sb.append("<input type=\"hidden\" name=\"").append(CommonConstants.SESSION_KEY_TOKEN)
            .append("\" value=\"").append(tokenStr).append("\" >");
        return sb.toString();
    }
}
